<?php

namespace App\Controllers;

class Base extends \Base\Controller\Admin
{

    public function __construct(\Windward\Core\Container $container)
    {
        parent::__construct($container);
        $container->set('club_no', $this->getClubNo());
    }
    
    public function getClubNo()
    {
	    $host = $_SERVER['HTTP_HOST'];
	    $tmp = explode('.', $host);
	    $clubNo = $tmp[0];
	    return $clubNo;
    }

    public function getCodeAction()
    {
        $mobile = $this->request->getQuery('mobile');
        $regex = '/^1[0-9]{10}$/';
        if (preg_match($regex, $mobile)) {
            $sms = new \Base\Lib\Sms\Eucp($this->container, \ConstConfig::SMS_CDKEY, \ConstConfig::SMS_PASSWORD);
            $code = rand(1000, 9999);
            $_SESSION['sms_code'] = [
                'code' => md5($code.$mobile),
                'time' => time() + 5 * 60
            ];
            if ($sms->send($mobile, '您本次登录的验证码为：' . $code.'，五分钟内有效')) {
                return $this->success();
            }
        }
        return $this->error();
    }

}
